package com.beaven.model.utils;

import android.support.v7.util.DiffUtil;
import java.util.List;

/**
 * author: Beaven
 * time: 2017/5/18 11:35
 */

public class DiffUiDataCallback<T extends DiffUiDataCallback.UiDataDiffer<T>>
    extends DiffUtil.Callback {

  private List<T> oldList;
  private List<T> newList;

  public DiffUiDataCallback(List<T> oldList, List<T> newList) {
    this.oldList = oldList;
    this.newList = newList;
  }

  @Override public int getOldListSize() {
    return oldList.size();
  }

  @Override public int getNewListSize() {
    return newList.size();
  }

  @Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
    T oldBean = oldList.get(oldItemPosition);
    T newBean = newList.get(newItemPosition);
    return newBean.isSame(oldBean);
  }

  @Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
    T oldBean = oldList.get(oldItemPosition);
    T newBean = newList.get(newItemPosition);
    return newBean.isUiContentSame(oldBean);
  }

  // 进行比较的数据类型
  public interface UiDataDiffer<T> {
    // 传递一个旧的数据给你，问你是否和你标示的是同一个数据
    boolean isSame(T old);

    // 你和旧的数据对比，内容是否相同
    boolean isUiContentSame(T old);
  }
}
